package com.example.finalproject.bean;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

public class LoginBean {
    private String userName;
    private String password;

    public LoginBean(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "LoginBean{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public static void saveLoginBean(Context context, String username, String password) {
        SharedPreferences shp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shp.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.apply();
    }

    public static Map<String, String> getLoginBean(Context context) {
        SharedPreferences shp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String username = shp.getString("username","");
        String password = shp.getString("password","");
        if (username.isEmpty() || password.isEmpty()) {
            return null;
        }

        Map<String, String> userMap = new HashMap<>();
        userMap.put("username", username);
        userMap.put("password", password);
        return userMap;
    }

    public static void clearLoginBean(Context context) {
        SharedPreferences shp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shp.edit();
        editor.remove("username");
        editor.remove("password");
        editor.apply();
    }
}
